import React from 'react';
import {connect} from 'dva'
import {Row, Col, Form, Select, message} from 'antd';
import Constants from '../../utils/Constants';
const R = require('ramda');
const FormItem = Form.Item;

export default connect(({logisticsSupplier, customerManage, customs }) => ({logisticsSupplier, customerManage, customs }))(Form.create({wrappedComponentRef: true})(class AccountInfos extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: true,
            type: "",

        }
    }


    handleResetFields = () => {
        this.props.form.resetFields();
    }

    handleOpen = (type) => {
        this.state.type = type;
        this.setState({open: true})
    }

    setStatementType = (value) => {
        let stObj = this.props.form.getFieldValue("stObj");
        this.props.setStatementType(stObj, value);
    }

    handleConfirm = () => {
        let flag = false;
        this.props.form.validateFields((errors, values) => {
            if (errors) return;

            flag = true;
            this.props.setStatementType(values.stObj, values.stObjId);
        })

        return flag;
    }

        getProvider = (e) => {
            this.props.form.setFieldsValue({stObjId: ''});
            let values = this.props.form.getFieldsValue();
            let type = R.isNil(e) ? values.stObj : e;
            if(type === '1'){
                this.props.dispatch({type: 'logisticsSupplier/getSupplierList'});
            }else if(type === '2'){
                this.props.dispatch({ type: 'customs/getListForPage', payload: { page: Constants.DEFAULT_PAGE } }); //查询海关信息
            }
        };


    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {labelCol: {xs: {span: 8}, sm: {span: 8}}, wrapperCol: {xs: {span: 15}, sm: {span: 14}}};
        let suppilerList = [];
        let wlsuppilerList = R.isNil(this.props.logisticsSupplier.supplierList) ? [] : this.props.logisticsSupplier.supplierList;
        let hgsuppilerList = R.isNil(this.props.customs.list) ? [] : this.props.customs.list;


        let stObjType = this.props.form.getFieldValue('stObj');
        const list = [[], wlsuppilerList, hgsuppilerList];
        suppilerList = list[stObjType];


        return (

            <Form>
                <Row>
                    <Col span={11}>
                        <FormItem {...formItemLayout} label="结算对象类型" hasFeedback>
                            {getFieldDecorator('stObj', {
                                rules: [{
                                    required: true, message: '请选择结算对象类型'
                                }],
                                initialValue: ''
                            })(
                                <Select style={{width: '100%'}} disabled={this.props.disabled} onChange={value => this.getProvider(value)}>
                                    <Select.Option value={"1"}>物流供应商结算</Select.Option>
                                    <Select.Option value={"2"}>海关结算</Select.Option>
                                </Select>
                            )}
                        </FormItem>
                    </Col>
                    <Col span={12}>
                        <FormItem {...formItemLayout} label="结算对象" hasFeedback>
                            {getFieldDecorator('stObjId', {
                                rules: [{
                                    required: true, message: '请选择结算对象'
                                }],
                                initialValue: ''
                            })(
                                <Select style={{width: '100%'}} disabled={this.props.disabled} onChange={this.setStatementType.bind(this)}
                                        showSearch allowClear
                                        placeholder="可通过输入关键字搜索"
                                        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                    {
                                        !R.isNil(suppilerList) ? suppilerList.map(item => {
                                            return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.companyname}</Select.Option>);
                                        }) : ''
                                    }
                                </Select>
                            )}
                        </FormItem>
                    </Col>
                </Row>
            </Form>
    );
    }
    })
    )

    // WEBPACK FOOTER //
    // ./public/src/components/order/components/AccountInfos.js